package com.unicom.projt;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.unicom.projt.account.RetrievePasswordStepOnePage;
import com.unicom.projt.db.Account;
import com.unicom.projt.db.DB;
import com.unicom.projt.reg.RegisterStepOnePage;
import com.unicom.projt.util.*;

public class Main extends Activity implements View.OnClickListener {


    private EditText mPhoneNumber;
    private EditText mPassword;

    private Handler mHandler;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 1;
    private MainController mController;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        mPhoneNumber = (EditText) this.findViewById(R.id.main_phone_number);
        mPassword = (EditText) this.findViewById(R.id.main_password);
        mPhoneNumber.setText(UserHolder.phoneNumber);
//        mPhoneNumber.setEnabled(false);

        findViewById(R.id.main_back_btn).setOnClickListener(this);
        findViewById(R.id.main_logout_btn).setOnClickListener(this);
        findViewById(R.id.main_login_btn).setOnClickListener(this);
        findViewById(R.id.main_forget_btn).setOnClickListener(this);


        mController = new MainController(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_forget_btn:
                if ("".equals(mPhoneNumber.getText().toString().trim())) {
                    AlertDialogUtils.showConfirmDiaLog(this, "请输入手机号");
                    return;
                }
                if (!CheckUtils.isMobileNo(mPhoneNumber.getText().toString().trim())) {
                    AlertDialogUtils.showConfirmDiaLog(this, "请输入正确的手机号");
                    return;
                }
                UserHolder.phoneNumber = mPhoneNumber.getText().toString().trim();
                PageJumpUtils.jump(this, RetrievePasswordStepOnePage.class);
                this.finish();
                break;
            case R.id.main_login_btn:
                doLogin();
                break;
            case R.id.main_back_btn:
                backToFirstPage();
                break;
            case R.id.main_logout_btn:
                backToFirstPage();
                break;
        }
    }


    private void backToFirstPage() {
        PageJumpUtils.jump(this, InputPhoneNumberPage.class);
        this.finish();
    }


    private void doLogin() {
        if ("".equals(mPhoneNumber.getText().toString().trim()) || "".equals(mPassword.getText().toString().trim())) {
            AlertDialogUtils.showConfirmDiaLog(this, "请输入手机号和密码");
            return;
        } else if (mPassword.getText().toString().trim().trim().length() < 6) {
            AlertDialogUtils.showConfirmDiaLog(this, "密码长度最少为6位");
            return;
        } else {
            if (!CheckUtils.isMobileNo(mPhoneNumber.getText().toString().trim())) {
                AlertDialogUtils.showConfirmDiaLog(this, "请输入正确的手机号");
                return;
            }
            showDialog(DIALOG_PROGRESS);
            String phoneNumber = mPhoneNumber.getText().toString().trim();
            String password = mPassword.getText().toString().trim();
            mController.login(phoneNumber, password);

            //
//            UserHolder.phoneNumber = phoneNumber;
//            UserHolder.password = password;
//            if (rememberMeChecked) {
//                mController.rememberUserInfo(phoneNumber, password);
//            }
//            PageJumpUtils.jump(this, PaymentSuccessPage.class);
//            this.finish();
        }

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        backToFirstPage();
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(Main.this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;

            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
